package news.dao;

import news.example.Publish;
import news.util.BaseDao;
import news.util.PageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JiangJ
 * @date 2022/3/2 20:48
 */
public class SelPublishImpl implements ISelPublishDao {
    BaseDao baseDao = new BaseDao();

    @Override
    public PageBean getNewsPublishPageList(PageBean pagebean) {
        //√
        List<Publish> list = null;
        try {
            baseDao.openDB();
            String sql = "SELECT id,theme,title,digest,username,createtime FROM  news_publish order by id LIMIT ?,?";
            baseDao.ps = baseDao.conn.prepareStatement(sql);
            baseDao.ps.setInt(1, pagebean.getStart());
            baseDao.ps.setInt(2, pagebean.getPageSize());
            baseDao.rs = baseDao.ps.executeQuery();

            list = new ArrayList<Publish>();
            while (baseDao.rs.next()) {
                Publish p = new Publish();
                p.setId(baseDao.rs.getInt("id"));
                p.setTheme(baseDao.rs.getString("theme"));
                p.setTitle(baseDao.rs.getString("title"));
                p.setDigest(baseDao.rs.getString("digest"));
                p.setUsername(baseDao.rs.getString("username"));
                p.setCreatetime(baseDao.rs.getString("createtime"));
                list.add(p);
            }
            pagebean.setObjectList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pagebean;
    }

    @Override
    public int calcTotalCount() {
        //√
        int count = 0;
        try {
            baseDao.openDB();
            String sql = "select count(1) from news_publish";
            baseDao.st = baseDao.conn.createStatement();
            baseDao.rs = baseDao.st.executeQuery(sql);
            while (baseDao.rs.next()) {
                count = baseDao.rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }


    @Override
    public List<Publish> findAllPublish() {
        //√
        List<Publish> list1 = new ArrayList<Publish>();
        try {
            baseDao.openDB();
            String sql = "SELECT id,theme,title,digest,username,createtime FROM  news_publish";
            baseDao.st = baseDao.conn.createStatement();
            baseDao.rs = baseDao.st.executeQuery(sql);

            while (baseDao.rs.next()) {
                Publish p1 = new Publish();
                p1.setId(baseDao.rs.getInt("id"));
                p1.setTheme(baseDao.rs.getString("theme"));
                p1.setTitle(baseDao.rs.getString("title"));
                p1.setDigest(baseDao.rs.getString("digest"));
                p1.setUsername(baseDao.rs.getString("username"));
                p1.setCreatetime(baseDao.rs.getString("createtime"));
                list1.add(p1);
            }
            System.out.println(list1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list1;
    }
}
